﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Mobile.Mvc;

namespace MVCTestClientContainer
{
    public partial class SearchForm : ViewForm, IView<Products>
    {
        [PublishEvent("OnSearch")]
        public event EventHandler<DataEventArgs<string>> OnSearchEvent;

        [PublishEvent("OnSelected")]
        public event EventHandler<DataEventArgs<NorthwindDataSet.ProductsRow>> OnSelectedEvent;

        [PublishEvent("OnBack")]
        public event EventHandler OnBackEvent;

        [PublishEvent("OnViewLoaded")]
        public event EventHandler ViewLoadedEvent;    
        
        public SearchForm()
        {
            InitializeComponent();           
        }

        private void OnProductsUpdated(object sender, EventArgs e)
        {           
            this.lstProducts.DataSource = this.ViewData.Model.ProductList.ToList();
            this.lstProducts.DisplayMember = "ProductName";
        }
              

        #region IView<Products> Members

        public new ViewDataDictionary<Products> ViewData
        {
            get;
            set;
        }

        public new Products Model
        {
            get;
            set;
        }

        #endregion

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            if (OnSearchEvent != null)
            {
                // Raise the event and notify the controller
                OnSearchEvent(this, new DataEventArgs<string>(txtSearch.Text));
            }        
        }

        private void menuItemBack_Click(object sender, EventArgs e)
        {
            if (this.OnBackEvent != null)
            {
                this.OnBackEvent(this, EventArgs.Empty);
            }
        }

        private void txtSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (OnSearchEvent != null)
            //{
            //    // Raise the event and notify the controller
            //    OnSearchEvent(this, new DataEventArgs<string>(txtSearch.Text));
            //}    
        }

        private void txtSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (OnSearchEvent != null)
            {
                // Raise the event and notify the controller
                OnSearchEvent(this, new DataEventArgs<string>(txtSearch.Text));
            }  
        }

        private void menuItemSelect_Click(object sender, EventArgs e)
        {
            if (this.lstProducts.SelectedItem != null)
            {
                NorthwindDataSet.ProductsRow product = this.lstProducts.SelectedItem as NorthwindDataSet.ProductsRow;
                if (this.OnSelectedEvent != null)
                {
                    this.OnSelectedEvent(this, new DataEventArgs<NorthwindDataSet.ProductsRow>(product));
                }
            }

            
        }

        private void SearchForm_Load(object sender, EventArgs e)
        {
            if (this.ViewLoadedEvent != null)
            {
                this.ViewLoadedEvent(this, EventArgs.Empty);
            }
        }

        #region IView<Products> Members


       

        #endregion
    }
}